# Zhou-Horiuchi, Studies 1–2 combined

# Initial settings --------------------------------------------------------

# dir.create("output/combined", recursive = TRUE, showWarnings = FALSE)
dir.create("figures/combined", recursive = TRUE, showWarnings = FALSE)
dir.create("logs", recursive = TRUE, showWarnings = FALSE)

source("functions/logging.R")
start_script_log("combined")

options(tidyverse.quiet = TRUE)
library(tidyverse)
library(estimatr)
library(ggthemes)
library(patchwork)

# Load data ---------------------------------------------------------------

out1 <- read_rds("output/study1/data_for_analysis.rds")
pp1 <- out1[[2]]

out2 <- read_rds("output/study2/data_for_analysis.rds")
pp2 <- out2[[2]]

profile_pairs <- bind_rows(pp1, pp2)

original_data <- bind_rows(out1[[3]], out2[[3]])

# Test pre-registered hypotheses ------------------------------------------

source("functions/test_hypotheses.R")
test_hypotheses(profile_pairs)[[1]]
ggsave("figures/combined/pre-registered_hypotheses.pdf", width = 8, height = 4)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/combined/pre-registered_hypotheses.pdf\n")
cat("------------------------------------------------------------\n\n")

# Comparing the Experiments -----------------------------------------------

test_hypotheses(profile_pairs)[[2]]
ggsave("figures/combined/mm_amce-by-experiment.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/combined/mm_amce-by-experiment.pdf\n")
cat("------------------------------------------------------------\n\n")

end_script_log()
